﻿using MusicPlayerPlus.Interface.Base;
using MusicPlayerPlus.Interface.Model.Muisc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AMing.Core.Extension;
using MusicPlayerPlus.Interface.Model.Enum;

namespace MusicPlayerPlus.PlugIn.MainWindows
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window,
        InterfaceBase,
        //Input
        MusicPlayerPlus.Interface.Base.Action.Input.IAutoExecuteAction,
        MusicPlayerPlus.Interface.Base.Action.Input.IAddToButtonAction,
        MusicPlayerPlus.Interface.Base.Action.Input.IAddToMenuAction,
        MusicPlayerPlus.Interface.Base.Action.Input.ITimeLineChangeAction,
        MusicPlayerPlus.Interface.Base.Action.Input.IMuiscListChange,
        MusicPlayerPlus.Interface.Base.Action.Input.IPlayMusicChange,
        MusicPlayerPlus.Interface.Base.Action.Input.IPlayListChange,
        MusicPlayerPlus.Interface.Base.Action.Input.IPlayOrderChangeAction,
        MusicPlayerPlus.Interface.Base.Action.Input.IPlayStateChangeAction,
        MusicPlayerPlus.Interface.Base.Action.Input.IVolumeChangeAction,
        MusicPlayerPlus.Interface.Base.Action.Input.IOrientationActionChange,
        //Output
        MusicPlayerPlus.Interface.Base.Action.Output.ISetMainWindow,
        MusicPlayerPlus.Interface.Base.Action.Output.IAddPlayListAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IChangeMusicListAction,
        MusicPlayerPlus.Interface.Base.Action.Output.ISetPlayMusicAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IChangeTimeLineAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IGetPlayListAction,

        MusicPlayerPlus.Interface.Base.Action.Output.IPlayAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IPauseAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IStopAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IPreviousTrackAction,
        MusicPlayerPlus.Interface.Base.Action.Output.INextTrackAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IChangeVolumeAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IPlayOrderChangeAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IOrientationAction
    {
        public MainWindow()
        {
            InitializeComponent();
            timeline.ShowValueConverter = (val) =>
            {
                TimeSpan timeSpan = TimeSpan.FromSeconds(val);
                return timeSpan.ToString_HHmmss();
            };
            vol.ShowValueConverter = (val) =>
            {
                return string.Format("{0}%", (int)(val * 100));
            };
        }

        #region Input

        /// <summary>
        /// 自动执行动作接口
        /// </summary>
        public void AutoExecute()
        {
            this.Show();
            if (SetMainWindow != null)
            {
                SetMainWindow(this);
            }
            cb_order.Items.Clear();
            foreach (var item in Enum.GetNames(typeof(MediaPlayPlayOrder)))
            {
                cb_order.Items.Add(item);
            }
            cb_order.SelectedIndex = 1;
            cb_order.SelectionChanged += cb_order_SelectionChanged_1;
        }
        /// <summary>
        /// 添加按钮动作接口
        /// </summary>
        public void AddToButton(string title, System.Action action)
        {
            Button btn = new Button() { Content = title };
            btn.Click += (s, e) =>
            {
                action();
            };
            sp_btns.Children.Add(btn);
        }
        /// <summary>
        /// 添加菜单动作接口
        /// </summary>
        public void AddToMenu(string title, System.Action action)
        {
            MenuItem mitem = new MenuItem() { Header = title };
            mitem.Click += (s, e) =>
            {
                action();
            };
            menu_temp.Items.Add(mitem);
        }
        /// <summary>
        /// (改变后事件)进度条改变
        /// </summary>
        /// <param name="max"></param>
        /// <param name="val"></param>
        public void TimeLineChange(double max, double val)
        {
            timeline.Max = max;
            timeline.Value = val;

            TimeSpan ts_max = TimeSpan.FromSeconds(max);
            TimeSpan ts_val = TimeSpan.FromSeconds(val);
            string name = "";
            if (playMusic != null)
            {
                name = playMusic.Name;
            }
            txt_info.Text = string.Format("[{3}]{0} -- {1} / {2}", name, ts_max.ToString_HHmmss(), ts_val.ToString_HHmmss(), mediaPlayState.ToString());
            //this.Title = string.Format("[{3}]{0} -- {1} / {2}", name, ts_max.ToString_HHmmss(), ts_val.ToString_HHmmss(), mediaPlayState.ToString());
        }
        /// <summary>
        /// 播放中的列表改变时
        /// </summary>
        public void MuiscListChange(Dictionary<Guid, MusicItem> list)
        {
            try
            {
                this.Dispatcher.BeginInvoke(new Action(() =>
                   {
                       listdata.Clear();
                       listbox.Items.Clear();
                       foreach (var item in list)
                       {
                           ModelView.MusicItemVM itemVM = new ModelView.MusicItemVM(item.Value);
                           listdata.Add(item.Value);
                           listbox.Items.Add(itemVM);
                       }
                   }));
            }
            catch (Exception ex)
            {
            }
        }
        /// <summary>
        /// 播放的歌曲改变时
        /// </summary>
        public void PlayMusicChange(MusicItem item)
        {
            playMusic = item;
            #region MyRegion

            int indexof = listdata.IndexOf(item);
            if (indexof >= 0)
            {
                int index = 0;
                foreach (var listbox_item in listbox.Items)
                {
                    ModelView.MusicItemVM itemVM = listbox_item as ModelView.MusicItemVM;
                    if (itemVM != null)
                    {
                        if (indexof == index)
                        {
                            itemVM.Play = true;
                            listbox.ScrollIntoView(listbox_item);
                        }
                        else
                        {
                            itemVM.Play = false;
                        }
                    }
                    index++;
                }
            }
            if (playMusic != null)
            {
                img_cove.Source = new BitmapImage(new Uri(playMusic.CoveHD));
            }

            #endregion
        }
        /// <summary>
        /// 播放列表改变时
        /// </summary>
        public void PlayListChange(Guid guid, PlayList list)
        {
            try
            {
                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    IList<PlayList> newlist = GetPlayList_Action();
                    playlist.Clear();
                    listbox_playlist.Items.Clear();
                    foreach (var item in newlist)
                    {
                        playlist.Add(item);
                        listbox_playlist.Items.Add(item.Name);
                    }
                }));
            }
            catch (Exception ex)
            {
            }
        }
        /// <summary>
        /// 播放状态改变
        /// </summary>
        public void PlayStateChange(MediaPlayState val)
        {
            mediaPlayState = val;
            if (mediaPlayState == MediaPlayState.Play)
            {
                btn_play.Content = "暂停";
            }
            else
            {
                btn_play.Content = "播放";
            }
        }
        /// <summary>
        /// (改变后事件)音量改变
        /// </summary>
        public void VolumeChange(double val)
        {
            vol.Value = val;
        }
        /// <summary>
        /// 播放顺序改变
        /// </summary>
        public void PlayPlayOrderChange(MediaPlayPlayOrder val)
        {

        }


        /// <summary>
        /// 定向通道方法Key
        /// </summary>
        public string OrientationAction_Key { get { return "MainWindow"; } }
        /// <summary>
        /// 定向通道方法
        /// </summary>
        public void OrientationActionChange(string key, object obj)
        {
            switch (key)
            {
                case "showorhide":
                    if (this.WindowState == System.Windows.WindowState.Minimized)
                    {
                        this.WindowState = System.Windows.WindowState.Normal;
                    }
                    else
                    {
                        this.WindowState = System.Windows.WindowState.Minimized;
                    }
                    break;
                case "show":
                    this.WindowState = System.Windows.WindowState.Normal;
                    break;
                case "hide":
                    this.WindowState = System.Windows.WindowState.Minimized;
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region Output
        /// <summary>
        /// 设置主窗体
        /// </summary>
        public Action<object> SetMainWindow { private get; set; }
        /// <summary>
        /// 添加播放列表
        /// </summary>
        public Func<PlayList, bool> AddPlayList_Action { private get; set; }
        /// <summary>
        /// 改变列表
        /// </summary>
        public Action<Guid> ChangeMusicList_Action { private get; set; }
        /// <summary>
        /// 设置播放的音乐
        /// </summary>
        public Action<MusicItem> SetPlayMusic_Action { private get; set; }
        /// <summary>
        /// (主动)进度条改变
        /// </summary>
        public Action<double> ChangeTimeLine_Action { private get; set; }
        /// <summary>
        /// 获取所有的播放列表
        /// </summary>
        public Func<IList<PlayList>> GetPlayList_Action { private get; set; }
        /// <summary>
        /// 播放
        /// </summary>
        public System.Action Play_Action { private get; set; }
        /// <summary>
        /// 暂停
        /// </summary>
        public System.Action Pause_Action { private get; set; }
        /// <summary>
        /// 停止
        /// </summary>
        public System.Action Stop_Action { private get; set; }
        /// <summary>
        /// 上一首
        /// </summary>
        public System.Action PreviousTrack_Action { private get; set; }
        /// <summary>
        /// 下一首
        /// </summary>
        public System.Action NextTrack_Action { private get; set; }
        /// <summary>
        /// (主动)音量改变
        /// </summary>
        public Action<double> ChangeVolume_Action { private get; set; }
        /// <summary>
        /// 播放顺序改变
        /// </summary>
        public Action<MediaPlayPlayOrder> PlayPlayOrderChange_Action { private get; set; }
        /// <summary>
        /// 触发定向通道
        /// </summary>
        public Action<string, string, object> OrientationAction_Action { private get; set; }
        #endregion

        #region Object

        IList<MusicItem> listdata = new List<MusicItem>();
        IList<PlayList> playlist = new List<PlayList>();
        MediaPlayState mediaPlayState = MediaPlayState.End;
        private MusicItem playMusic = null;
        #endregion

        #region Event

        private void listbox_MouseDoubleClick_1(object sender, MouseButtonEventArgs e)
        {
            int index = listbox.SelectedIndex;
            if (index >= 0 && index < listdata.Count)
            {
                MusicItem item = listdata[index];
                SetPlayMusic_Action(item);
            }
        }

        private void timeline_ChangeValue_1(double obj)
        {
            ChangeTimeLine_Action(obj);
        }

        private void listbox_playlist_MouseDoubleClick_1(object sender, MouseButtonEventArgs e)
        {
            int index = listbox_playlist.SelectedIndex;
            if (index >= 0 && index < playlist.Count)
            {
                PlayList item = playlist[index];
                ChangeMusicList_Action(item.ID);
            }
        }

        private void vol_ChangeValue_1(double obj)
        {
            ChangeVolume_Action(obj);
        }

        private void btn_pre_Click_1(object sender, RoutedEventArgs e)
        {
            PreviousTrack_Action();
        }

        private void btn_play_Click_1(object sender, RoutedEventArgs e)
        {
            if (mediaPlayState == MediaPlayState.Play)
            {
                Pause_Action();
            }
            else
            {
                Play_Action();
            }
        }

        private void btn_stop_Click_1(object sender, RoutedEventArgs e)
        {
            Stop_Action();
        }

        private void btn_next_Click_1(object sender, RoutedEventArgs e)
        {
            NextTrack_Action();
        }

        private void cb_order_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                string data = cb_order.SelectedItem as string;
                MediaPlayPlayOrder order = (MediaPlayPlayOrder)Enum.Parse(typeof(MediaPlayPlayOrder), data, true);
                PlayPlayOrderChange_Action(order);
            }
            catch (Exception ex)
            {
            }

        }

        #endregion

        private void Window_Closed_1(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void menu_main_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }
    }
}
